//package com.want.method.handler;
//
//import com.want.client.Client;
//import com.want.method.MethodInfo;
//import com.want.service.ServiceInfo;
//import com.want.util.UriUtil;
//import org.springframework.web.reactive.function.BodyInserters;
//import org.springframework.web.reactive.function.client.ClientRequest;
//import org.springframework.web.reactive.function.client.WebClient;
//import org.springframework.web.util.DefaultUriBuilderFactory;
//
//import java.net.URI;
//import java.util.Optional;
//
///**
// * @author WangZhiJian
// * @since 2021/12/4 0004
// */
//public interface WantMethodHandler {
//    /**
//     * 执行操作
//     * @param methodInfo
//     * @return
//     */
//    <T,V> Object invokeRest(ServiceInfo serviceInfo,MethodInfo<T, V> methodInfo) throws Throwable;
//
//    public static class DefaultWantMethodHandler implements WantMethodHandler {
//
//        private Client client;
//        DefaultUriBuilderFactory defaultUriBuilderFactory = new DefaultUriBuilderFactory();
//        public DefaultWantMethodHandler(Client client) {
//            this.client = client;
//        }
//        public DefaultWantMethodHandler() {
//            this.client = new Client.DefaultClient();
//        }
//
//
//        @Override
//        public <T,V> Object invokeRest(ServiceInfo serviceInfo,MethodInfo<T,V> methodInfo) throws Throwable {
//            String uri = methodInfo.getUrl();
//            if(serviceInfo != null && serviceInfo.getBaseUrl() != null)
//                uri = UriUtil.contactPath(serviceInfo.getBaseUrl(),uri);
//            URI targetUri;
//            if(methodInfo.getParams() != null){
//                targetUri = defaultUriBuilderFactory.expand(uri,methodInfo.getParams());
//            }else {
//                targetUri = new URI(uri);
//            }
//
//            ClientRequest.Builder clientBuilder = ClientRequest.create(methodInfo.getMethod(),targetUri);
//
//            Optional.ofNullable(methodInfo.getBody())
//                    .map(body -> BodyInserters.fromPublisher(body,methodInfo.getBodyElementType()))
//                    .ifPresent(clientBuilder::body);
//
//            WebClient.ResponseSpec responseSpec = client.exec(clientBuilder.build());
//
//            if(methodInfo.isReturnFlux())
//                return responseSpec.bodyToFlux(methodInfo.getReturnElementType());
//
//            return responseSpec.bodyToMono(methodInfo.getReturnElementType());
//        }
//    }
//}
